from flask_restful import Resource
from flask_cors import CORS,cross_origin

from flask_shop.menu import menu_api,menu_bp
from flask_shop import models
from flask import request


class Menus(Resource):
  def get(self):
    #获取前端要求的type值 list or tree
    type_=request.args.get('type')
    if type_=='tree':
        # 通过模型获取数据
        menu_list = models.Menu.query.filter(models.Menu.level==1).all()
        menu_data = []
        # 遍历数据
        for m in menu_list:
            menu_data.append(m.to_dict_tree())
        return {'status':200,'msg':'获取菜单成功','data':menu_data}
    else:
        menu_list=models.Menu.query.filter(models.Menu.level!=0).all()
        menu_data=[]
        for m in menu_list:
            menu_data.append(m.to_dict_list())
        return {'status':200,'msg':'获取菜单成功','data':menu_data}

menu_api.add_resource(Menus,'/menus/')


@menu_bp.route('/test/')
def test():
   return {
      'msg':'123'
   }